package me.firstapp.weixin.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import me.firstapp.common.utils.CookieUtils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.common.utils.WeiXinConfig;


/**
 * COOKIE用户工具类
 * 
 */
public class CookieMemberUtils {

	private static final Log log = LogFactory.getLog(CookieMemberUtils.class);

	public static final String BETATOWN_SESSION_DOMAIN = WeiXinConfig.CONFIGRELOAD.getProp().getProperty("session_domain");

	public static final String WEIXIN_AUTH_STATES = WeiXinConfig.CONFIGRELOAD.getProp().getProperty("weixin.auth.states");

	public static final String OPENIDKEY = "gaxaogei1";
	public static final String MEMBERIDKEY = "tgadafvaf";
	public static final String LOGINTOKENKEY = "sga33sdfg";

	/**
	 * 接口调用前读取openId
	 * 
	 * @return
	 */
	public static CookieMember readCookieWeiXinMember(HttpServletRequest request) {

		// 获取请求中openId
		String openId = request.getParameter("openId");

		CookieMember cookieMember = null;

		if (StrUtils.isNULL(openId)) {
			// url请求中没有openId，尝试从cookie中获取
			Cookie openCookie = CookieUtils.getCookie(request, OPENIDKEY);
			if (openCookie != null) {
				cookieMember = new CookieMember();
				cookieMember.setOpenId(openCookie.getValue());
				cookieMember.setIsNewMember(false);
			}
		} else {
			// url请求中存在openId，则该请求是用户新认证
			cookieMember = new CookieMember();
			cookieMember.setIsNewMember(true);
			cookieMember.setOpenId(openId);
			/*
			 * 判断openId在数据库中是否有对应的memberId
			 * 
			 * 有：读取赋值 无：新建用户 且赋值
			 */
			// CrmWeixinBoundService crmWeixinBoundService =
			// SpringContextTool.getApplicationContext().getBean(CrmWeixinBoundService.class);
			// CrmWeixinBound crmWeixinBound =
			// crmWeixinBoundService.executeCheckORCreateCrmWeixinBound(openId);
			// weiXinMember.setMemberId(crmWeixinBound.getMemberId());
		}

		return cookieMember;
	}

	/**
	 * 接口调用前读取登錄用戶
	 * 
	 * @return
	 */
	public static CookieMember readCookieMember(HttpServletRequest request) {

		CookieMember cookieMember = null;

		Cookie openCookie = CookieUtils.getCookie(request, LOGINTOKENKEY);
		if (openCookie != null) {
			cookieMember = new CookieMember();
			cookieMember.setLoginToken(openCookie.getValue());
			cookieMember.setIsNewMember(false);
		}

		return cookieMember;
	}

	// /**
	// * 认证后写入用户
	// *
	// * @return
	// */
	// public static boolean writeWeiXinMember(CookieMember cookieMember,
	// HttpServletRequest request, HttpServletResponse response) {
	//
	// writeCookieMemberOpenId(cookieMember, request, response);
	// writeCookieMemberMemberId(cookieMember, request, response);
	// log.debug("用户认证信息写入Cookie成功");
	// return true;
	// }

	public static boolean writeCookieMemberOpenId(CookieMember cookieMember, HttpServletRequest request, HttpServletResponse response) {
		if (cookieMember != null && null != cookieMember.getOpenId() && cookieMember.getIsNewMember()) {
			CookieUtils.addCookie(request, response, BETATOWN_SESSION_DOMAIN, OPENIDKEY, cookieMember.getOpenId(), 365 * 24 * 60 * 60 * 1000);
			log.debug("用户openId写入Cookie成功");
			return true;
		} else {
			log.debug("用户openId写入Cookie失败");
			return false;
		}
	}

	public static boolean writeCookieMemberMemberId(CookieMember cookieMember, HttpServletRequest request, HttpServletResponse response) {
		if (cookieMember != null && null != cookieMember.getMemberId() && cookieMember.getIsNewMember()) {
			CookieUtils.addCookie(request, response, BETATOWN_SESSION_DOMAIN, MEMBERIDKEY, cookieMember.getMemberId(), 365 * 24 * 60 * 60 * 1000);
			log.debug("用户memberId写入Cookie成功");
			return true;
		} else {
			log.debug("用户memberId写入Cookie失败");
			return false;
		}
	}

	public static boolean writeCookieMemberLoginToken(CookieMember cookieMember, HttpServletRequest request, HttpServletResponse response) {
		if (cookieMember != null && null != cookieMember.getLoginToken() && cookieMember.getIsNewMember()) {
			CookieUtils.addCookie(request, response, BETATOWN_SESSION_DOMAIN, LOGINTOKENKEY, cookieMember.getLoginToken(), 365 * 24 * 60 * 60 * 1000);
			log.debug("用户loginToken写入Cookie成功");
			return true;
		} else {
			log.debug("用户loginToken写入Cookie失败");
			return false;
		}
	}

	public static boolean cookieMemberLoginOut(HttpServletRequest request, HttpServletResponse response) {
		CookieUtils.cancleCookie(response, LOGINTOKENKEY, BETATOWN_SESSION_DOMAIN);
		CookieUtils.cancleCookie(response, MEMBERIDKEY, BETATOWN_SESSION_DOMAIN);
		CookieUtils.cancleCookie(response, OPENIDKEY, BETATOWN_SESSION_DOMAIN);
		request.removeAttribute("loginToken");
		request.getSession().removeAttribute("loginToken");
		request.removeAttribute("memberId");
		request.getSession().removeAttribute("memberId");
		// Cookie a = CookieUtils.getCookie(request, OPENIDKEY);
		// Cookie b = CookieUtils.getCookie(request, MEMBERIDKEY);
		// Cookie c = CookieUtils.getCookie(request, LOGINTOKENKEY);
		return true;
	}
}
